package cn.aitrox.ry.util;

import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串操作，含数字类型转换
 *
 */
public class StringUtil {
    private static final Pattern CRLF = Pattern.compile("(\r\n|\r|\n|\n\r)");

    /**
     * 过滤回车
     */
    public static String filterReturn(String text) {
        Matcher m = CRLF.matcher(text);
        if (m.find())
            text = m.replaceAll("");
        return text;
    }

    /**
     * 过滤回车和前后空格
     */
    public static String purify(String text) {
        if (Strings.isNullOrEmpty(text))
            return text;
        else
            return filterReturn(text.trim());
    }

    public static String substring(String str, int beginIndex, int endIndex) {
        if (str == null || beginIndex < 0 || endIndex < 0)
            return str;
        int maxIndex = str.length();
        return str.substring(Math.min(beginIndex, maxIndex), Math.min(endIndex, maxIndex));
    }

    /**
     * 用*替换
     * 
     * @param str
     * @param index 从0开始
     * @return
     */
    public static String maskStar(String str, int index) {
        if (str == null || index < 0)
            return str;
        int length = str.length() - index;
        return _maskStar(str, index, length);
    }

    /**
     * 用*替换
     * 
     * @param str
     * @param index 从0开始
     * @param length
     * @return
     */
    public static String maskStar(String str, int index, int length) {
        if (str == null || index < 0 || length <= 0 || str.length() < index + length)
            return str;
        return _maskStar(str, index, length);
    }

    private static String _maskStar(String str, int index, int length) {
        String stars = Strings.repeat("*", length);
        str = String.format("%s%s%s", str.substring(0, index), stars, str.substring(index + length));
        return str;
    }

    public static String genProgressString(int current, int total) {
        float progress = (float) current / total * 100;
        return String.format("%s/%s=%.2f%%", current, total, progress);
    }

    public static String valueOf(Integer val) {
        if (val == null)
            return null;
        else
            return String.valueOf(val);
    }

    public static String valueOf(Double val) {
        if (val == null)
            return null;
        else
            return String.valueOf(val);
    }

    public static String valueOf(Object obj) {
        return obj != null ? obj.toString() : null;
    }

    public static String toString(Integer val) {
        if (val == null)
            return null;

        return String.valueOf(val.intValue());
    }

    public static boolean equal(String text1, String text2) {
        return text1 == null ? text2 == null : text1.equals(text2);
    }

    /*
     * 判断字符串是否为空。
     */
    public static boolean isEmpty(String value) {
        return (value == null || value.trim().equals(""));
    }

    /**
     * 字符串split到List
     * @param value [59469,59587,12580,12711]
     * @return
     */
    public static List<String> split2Array(String value) {
        value = value.trim();
        if (value.startsWith("["))
            value = value.substring(1);
        if (value.endsWith("]"))
            value = value.substring(0, value.length() - 1);

        return Splitter.on(",").omitEmptyStrings().splitToList(value);
    }

    public static String subStr(String str, int subLimit) {
        if(StringUtils.isEmpty(str) || subLimit <= 0) {
            return "";
        }
        if(str.length() > subLimit) {
            return str.substring(0,subLimit);
        }
        return str;
    }
}
